﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WSAP;
using PGMHelper;

namespace CouplingBeamStiff
{
    class ComponentBasic
    {
        /// <summary>
        /// 层号
        /// </summary>
        public int StoreyId { get; set; }

        /// <summary>
        /// 左墙宽度
        /// </summary>
        protected double LeftWallWidth { get; set; }

        /// <summary>
        /// 右墙宽度
        /// </summary>
        protected double RightWallWidth { get; set; }

        /// <summary>
        /// 层高
        /// </summary>
        protected double StoryHeight { get; set;}

        /// <summary>
        /// 连梁宽度
        /// </summary>
        protected double MiddleBeamWidth { get; set; }

        /// <summary>
        /// 连梁高度
        /// </summary>
        protected double MiddleBeamHeight { get; set; }

        /// <summary>
        /// 壳单元集合
        /// </summary>
        protected List<Block> BlockList { get; set; }

        /// <summary>
        /// 虚方法划分网格
        /// </summary>
        public virtual void Mesh(double Yoffset)
        {

        }

        public double GetBeamHeight()
        {
            return this.MiddleBeamHeight;
        }

        /// <summary>
        /// 添加节点荷载
        /// </summary>
        public void AddNodeLoad(WModelNet model, List<Node> targetNodeList, double loadFactor)
        {
            var nodeList = this.GetNodeList();
            var ymax = (from node in nodeList select node.Y).ToList().Max();
            var topNodeList = targetNodeList.Find("Y", ymax);
            topNodeList.ForEach(node => model.AddNodeLoad(node.Index, 1, loadFactor * node.Y / topNodeList.Count));
        }

        /// <summary>
        /// 添加单元集合
        /// </summary>
        /// <returns></returns>
        public bool AddElements(WModelNet model)
        {
            foreach(var block in this.BlockList)
            {
                if (!block.AddBlock(model))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 获得单个构件的所有节点
        /// </summary>
        /// <returns></returns>
        public List<Node> GetNodeList()
        {
            List<Node> nodelist = new List<Node>();
            foreach(var block in this.BlockList)
            {
                nodelist.AddRange(block.GetNodelist());
            }
            return nodelist;
        }
        public double GetStoryHeight()
        {
            return this.StoryHeight;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="leftWallWidth"></param>
        /// <param name="rightWallWidth"></param>
        /// <param name="height"></param>
        /// <param name="middleBeamWidth"></param>
        /// <param name="middleBeamHeight"></param>
        public ComponentBasic(int storeyId,double leftWallWidth,double rightWallWidth,double height,double middleBeamWidth,double middleBeamHeight)
        {
            this.StoreyId = storeyId;
            this.LeftWallWidth = leftWallWidth;
            this.RightWallWidth = rightWallWidth;
            this.StoryHeight = height;
            this.MiddleBeamHeight = middleBeamHeight;
            this.MiddleBeamWidth = middleBeamWidth;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="basic"></param>
        public ComponentBasic(ComponentBasic basic)
        {
            this.StoreyId = basic.StoreyId;
            this.LeftWallWidth = basic.LeftWallWidth;
            this.RightWallWidth = basic.RightWallWidth;
            this.MiddleBeamWidth = basic.MiddleBeamWidth;
            this.MiddleBeamHeight = basic.MiddleBeamHeight;
            this.StoryHeight = basic.StoryHeight;
        }

    }
    /// <summary>
    /// 连肢剪力墙类(中梁为壳单元)
    /// </summary>
    class CoupledShearWall:ComponentBasic
    {
        public CoupledShearWall(int storeyId,double leftWallWidth, double rightWallWidth, double height, double middleBeamWidth, double middleBeamHeight)
            :base(storeyId,leftWallWidth, rightWallWidth, height,  middleBeamWidth, middleBeamHeight)
        {

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public CoupledShearWall(ComponentBasic basic, double middleBeamHeigth)
            : base(basic)
        {
            this.MiddleBeamHeight = middleBeamHeigth;
        }
        /// <summary>
        /// 重写划分网格方法
        /// </summary>
        public override void Mesh(double Yoffset)
        {
            var Concrete = new Material(GlobalVar.ConcreteYoungsModulus,GlobalVar.ConcretePossionRatio);
            this.BlockList = new List<Block>()
            {
                new Block(1,LeftWallWidth,StoryHeight-MiddleBeamHeight,0,Yoffset,Concrete,false),
                new Block(2,LeftWallWidth,MiddleBeamHeight,0,Yoffset+StoryHeight-MiddleBeamHeight,Concrete,false),

                new Block(3,MiddleBeamWidth,MiddleBeamHeight,
                LeftWallWidth,
                Yoffset+StoryHeight-MiddleBeamHeight,Concrete,false),

                new Block(4,RightWallWidth,MiddleBeamHeight,LeftWallWidth+MiddleBeamWidth,Yoffset+StoryHeight-MiddleBeamHeight,Concrete,false),
                new Block(5,RightWallWidth,StoryHeight-MiddleBeamHeight,LeftWallWidth+MiddleBeamWidth,Yoffset,Concrete,false)
            };
            this.BlockList.ForEach(block =>
            {
                block.Mesh();
            });

        }
    }
    /// <summary>
    /// 连肢剪力墙类(中梁梁单元)
    /// </summary>
    class NewCoupledShearWall : ComponentBasic
    {
        public NewCoupledShearWall(int storeyId, double leftWallWidth, double rightWallWidth, double height, double middleBeamWidth, double middleBeamHeight)
            : base(storeyId, leftWallWidth, rightWallWidth, height, middleBeamWidth, middleBeamHeight)
        {

        }
        /// <summary>
        /// 重写划分网格方法
        /// </summary>
        public override void Mesh(double Yoffset)
        {
            var Concrete = new Material(GlobalVar.ConcreteYoungsModulus, GlobalVar.ConcretePossionRatio);
            this.BlockList = new List<Block>()
            {
                new Block(1,LeftWallWidth,StoryHeight-MiddleBeamHeight,0,Yoffset,Concrete,false),
                new Block(2,LeftWallWidth,MiddleBeamHeight,0,Yoffset+StoryHeight-MiddleBeamHeight,Concrete,false),

                new Block(3,MiddleBeamWidth,MiddleBeamHeight,LeftWallWidth,
                Yoffset + StoryHeight, Concrete, true),

                new Block(4,RightWallWidth,MiddleBeamHeight,LeftWallWidth+MiddleBeamWidth,Yoffset+StoryHeight-MiddleBeamHeight,Concrete,false),
                new Block(5,RightWallWidth,StoryHeight-MiddleBeamHeight,LeftWallWidth+MiddleBeamWidth,Yoffset,Concrete,false)
            };
            this.BlockList.ForEach(block =>
            {
                block.Mesh();
            });

        }
    }
}
